<template>
  <a-card :bordered="false">
    <a-spin :spinning="confirmLoading">
      <div style="display: flex">
        <div style="width: 100px; line-height: 32px; font-weight: bold; color: #000">{{ $t('兑换规则') }}</div>
        <j-form-container>
          <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
            <a-row :gutter="[0, 24]">
              <a-col :span="24">
                <a-form-model-item style="margin-bottom: 0px">
                  <a-checkbox v-model="model.coinExchangeValid" >{{ $t('币兑换') }}</a-checkbox>
                  <a-input-number v-enable-if="'GiftWorkunitsController:edit'" v-model="model.coinExchange" :placeholder="$t('请输入币兑换')" style="width: 150px"

                                  :formatter="formatter"
                                  :parser="parser"
                                  />
                  <span style="margin-left: 5px">{{ $t('币等于1元') }}</span>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item style="margin-bottom: 0px">
                  <a-checkbox v-model="model.ticketExchangeValid">{{ $t('彩票兑换') }}</a-checkbox>
                  <a-input-number
                    v-model="model.ticketExchange"
                    :placeholder="$t('请输入彩票兑换')"
                    style="width: 150px"

                    :formatter="formatter"
                                  :parser="parser"

                  />
                  <span style="margin-left: 5px">{{ $t('彩票等于1元') }}</span>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item style="margin-bottom: 0px">
                  <a-checkbox v-model="model.pointExchangeValid">{{ $t('积分兑换') }}</a-checkbox>
                  <a-input-number
                    v-model="model.pointExchange"
                    :placeholder="$t('请输入积分兑换')"
                    style="width: 150px"

                    :formatter="formatter"
                                  :parser="parser"

                  />
                  <span style="margin-left: 5px">{{ $t('积分等于1元') }}</span>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item style="margin-bottom: 0px">
                  <a-checkbox v-model="model.acoinExchangeValid">{{ $t('A币兑换') }}</a-checkbox>
                  <a-input-number
                    v-model="model.acoinExchange"
                    :placeholder="$t('请输入A币兑换')"
                    style="width: 150px"

                    :formatter="formatter"
                                  :parser="parser"

                  />
                  <span style="margin-left: 5px">{{ $t('A币等于1元') }}</span>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item style="margin-bottom: 0px">
                  <a-checkbox v-model="model.aticketExchangeValid">{{ $t('A票兑换') }}</a-checkbox>
                  <a-input-number
                    v-model="model.aticketExchange"
                    :placeholder="$t('请输入A票兑换')"
                    style="width: 150px"

                    :formatter="formatter"
                                  :parser="parser"

                  />
                  <span style="margin-left: 5px">{{ $t('A票等于1元') }}</span>
                </a-form-model-item>
              </a-col>
              <a-col :span="24" style="text-align: center">
                <a-row>
                  <a-col :xs="24" :sm="3" style="text-align: right">
                    <a-button v-has="'GiftWorkunitsController:edit'" type="primary" style="margin-left: 20px" :disabled="confirmLoading" @click="submitForm">{{
                      $t('保存')
                    }}</a-button>
                  </a-col>
                </a-row>
              </a-col>
            </a-row>
          </a-form-model>
        </j-form-container>
      </div>
      <div style="display: flex; margin-top: 20px">
        <div style="width: 100px; line-height: 32px; font-weight: bold; color: #000">{{ $t('兑换限制') }}</div>
        <a-row :gutter="[0, 24]">
          <a-col :span="24">
            <a-checkbox v-model="giftExchangeLimit.ivalue">{{ $t('价格限制') }}</a-checkbox>
            <a-input-number
              v-model="giftExchangeLimitMin.ivalue"
              style="width: 130px; margin-left: 10px"
              :disabled="!giftExchangeLimit.ivalue"

              :min="0"
              :max="999999"

              :formatter="formatter"
                                  :parser="parser"
              :precision="0"
            />
            <span style="margin-left: 5px">~</span>
            <a-input-number
              v-model="giftExchangeLimitMax.ivalue"
              style="width: 130px; margin-left: 5px"
              :disabled="!giftExchangeLimit.ivalue"

              :min="0"
              :max="999999"

              :formatter="formatter"
                                  :parser="parser"

              :precision="0"
            />
            <a-button v-has="'GiftWorkunitsController:edit'" type="primary" style="margin-left: 20px" @click="giftExchangeLimitSet()">{{
              $t('保存')
            }}</a-button>
            <span style="color: orange; line-height: 41px; margin-bottom: 10px">
              {{ $t('（开启价格限制后该场地小程序上仅展示满足该价格区间的商品）') }}
            </span>
          </a-col>
          <a-col :span="24">
            <a-checkbox v-model="giftDayExMax.ivalue">{{ $t('每个会员每日允许兑换最大礼品价值') }}</a-checkbox>
            <a-input-number
              v-model="giftDayExMax.svalue"
              style="width: 130px"
              :disabled="!giftDayExMax.ivalue"

              :min="0"
              :max="999999"

              :formatter="formatter"
                                  :parser="parser"

              :precision="0"
            />
            <span style="margin-left: 5px">{{ $t('元') }}</span>
            <a-button v-has="'GiftWorkunitsController:edit'" type="primary" style="margin-left: 20px" @click="giftDayExMaxSet()">{{ $t('保存') }}</a-button>
          </a-col>
          <a-col :span="24">
            <a-checkbox v-model="giftDayOrderMax.ivalue">{{ $t('每个会员每日允许兑换最大次数') }}</a-checkbox>
            <a-input-number
              v-model="giftDayOrderMax.svalue"
              style="width: 130px"
              :disabled="!giftDayOrderMax.ivalue"
          
              :min="0"
              :max="999999"
          
              :formatter="formatter"
                                  :parser="parser"
          
              :precision="0"
            />
            <a-button v-has="'GiftWorkunitsController:edit'" type="primary" style="margin-left: 20px" @click="giftDayOrderMaxSet()">{{ $t('保存') }}</a-button>
          </a-col>
        </a-row>
      </div>
    </a-spin>
  </a-card>
</template>

<script>
import { getAction, putAction, httpAction } from '@/api/manage'
import axios from 'axios'
import {formatter, parser} from "@api/comm";

export default {
  name: 'ExchangeSettings',
  components: {},
  data() {
    return {
      formatter,
      parser,
      description: '兑换设置',
      model: {},
      confirmLoading: false,
      validatorRules: {},
      giftExchangeLimit: {
        name: 'giftExchangeLimit',
        ivalue: 0,
        comment: '价格限制',
      },
      giftExchangeLimitMin: {
        name: 'giftExchangeLimitMin',
        ivalue: null,
        comment: '价格限制',
      },
      giftExchangeLimitMax: {
        name: 'giftExchangeLimitMax',
        ivalue: null,
        comment: '价格限制',
      },
      giftDayExMax: {
        name: 'giftDayExMax',
        ivalue: 0,
        svalue: 2000,
        comment: '每个会员每日允许兑换最大礼品价值',
      },
      giftDayOrderMax: {
        name: 'giftDayOrderMax',
        ivalue: 0,
        svalue: 1,
        comment: '每个会员每日允许兑换最大次数',
      },
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
    this.list()
    this.giftExchangeLimitGet()
    this.giftDayExMaxGet()
    this.giftDayOrderMaxGet()
  },
  methods: {
    giftDayExMaxGet() {
      axios.all([this.getParameter('giftDayExMax')]).then(
        axios.spread((r1) => {
          if (r1.success && r1.result.total > 0) {
            this.giftDayExMax = r1.result.records[0]
          }
        })
      )
    },
    giftDayExMaxSet() {
      if (this.giftDayExMax.ivalue === true || this.giftDayExMax.ivalue === 1) {
        if (this.giftDayExMax.svalue == '') {
          this.$message.warning('每个会员每日允许兑换最大礼品价值未设置')
          return
        }
      }
      axios.all([this.setParameter('giftDayExMax')]).then(
        axios.spread((r1) => {
          if (r1.success) {
            this.giftDayExMaxGet()
            this.$message.success(r1.message)
          }
        })
      )
    },
    giftDayOrderMaxGet() {
      axios.all([this.getParameter('giftDayOrderMax')]).then(
        axios.spread((r1) => {
          if (r1.success && r1.result.total > 0) {
            this.giftDayOrderMax = r1.result.records[0]
          }
        })
      )
    },
    giftDayOrderMaxSet() {
      if (this.giftDayOrderMax.ivalue === true || this.giftDayOrderMax.ivalue === 1) {
        if (this.giftDayOrderMax.svalue == '') {
          this.$message.warning('每个会员每日允许兑换最大次数未设置')
          return
        }
      }
      axios.all([this.setParameter('giftDayOrderMax')]).then(
        axios.spread((r1) => {
          if (r1.success) {
            this.giftDayOrderMaxGet()
            this.$message.success(r1.message)
          }
        })
      )
    },
    giftExchangeLimitSet() {
      if (this.giftExchangeLimit.ivalue === true || this.giftExchangeLimit.ivalue === 1) {
        if (this.giftExchangeLimitMin.ivalue == '' || this.giftExchangeLimitMax.ivalue == '') {
          this.$message.warning('价格区间未设置')
          return
        }
      }
      axios
        .all([
          this.setGiftParameter('giftExchangeLimit'),
          this.setGiftParameter('giftExchangeLimitMin'),
          this.setGiftParameter('giftExchangeLimitMax'),
        ])
        .then(
          axios.spread((r1, r2, r3) => {
            if (r1.success && r2.success && r3.success) {
              this.giftExchangeLimitGet()
              this.$message.success(r1.message)
            }
          })
        )
    },
    giftExchangeLimitGet() {
      axios
        .all([
          this.getGiftParameter('giftExchangeLimit'),
          this.getGiftParameter('giftExchangeLimitMin'),
          this.getGiftParameter('giftExchangeLimitMax'),
        ])
        .then(
          axios.spread((r1, r2, r3) => {
            if (r1.success && r1.result.total > 0) {
              this.giftExchangeLimit = r1.result.records[0]
            }
            if (r2.success && r2.result.total > 0) {
              this.giftExchangeLimitMin = r2.result.records[0]
            }
            if (r3.success && r3.result.total > 0) {
              this.giftExchangeLimitMax = r3.result.records[0]
            }
          })
        )
    },
    setGiftParameter(name) {
      let querydata = {}
      let url = ''
      let method = ''
      if (
        (name === 'giftExchangeLimit' && this.giftExchangeLimit.id) ||
        (name === 'giftExchangeLimitMin' && this.giftExchangeLimitMin.id) ||
        (name === 'giftExchangeLimitMax' && this.giftExchangeLimitMax.id) ||
        (name === 'giftDayExMax' && this.giftDayExMax.id) ||
        (name === 'giftDayOrderMax' && this.giftDayOrderMax.id)
      ) {
        url = '/gift/parameter'
        method = 'put'
      } else {
        url = '/gift/parameter'
        method = 'post'
      }
      if (name === 'giftExchangeLimit') {
        if (this.giftExchangeLimit.ivalue === true || this.giftExchangeLimit.ivalue === 1) {
          this.giftExchangeLimit.ivalue = 1
        } else {
          this.giftExchangeLimit.ivalue = 0
        }
        querydata = Object.assign({}, this.giftExchangeLimit)
      }
      if (name === 'giftExchangeLimitMin') {
        querydata = Object.assign({}, this.giftExchangeLimitMin)
      }
      if (name === 'giftExchangeLimitMax') {
        querydata = Object.assign({}, this.giftExchangeLimitMax)
      }
      if (name === 'giftDayExMax') {
        if (this.giftDayExMax.ivalue === true || this.giftDayExMax.ivalue === 1) {
          this.giftDayExMax.ivalue = 1
        } else {
          this.giftDayExMax.ivalue = 0
        }
        querydata = Object.assign({}, this.giftDayExMax)
      }
      if (name === 'giftDayOrderMax') {
        if (this.giftDayOrderMax.ivalue === true || this.giftDayOrderMax.ivalue === 1) {
          this.giftDayOrderMax.ivalue = 1
        } else {
          this.giftDayOrderMax.ivalue = 0
        }
        querydata = Object.assign({}, this.giftDayOrderMax)
      }
      return httpAction(url, querydata, method)
    },
    setParameter(name) {
      let querydata = {}
      let url = ''
      let method = ''
      if (
        (name === 'giftExchangeLimit' && this.giftExchangeLimit.id) ||
        (name === 'giftExchangeLimitMin' && this.giftExchangeLimitMin.id) ||
        (name === 'giftExchangeLimitMax' && this.giftExchangeLimitMax.id) ||
        (name === 'giftDayExMax' && this.giftDayExMax.id) ||
        (name === 'giftDayOrderMax' && this.giftDayOrderMax.id)
      ) {
        url = '/bis/bisSysParameter/edit'
        method = 'put'
      } else {
        url = '/bis/bisSysParameter/add'
        method = 'post'
      }
      if (name === 'giftExchangeLimit') {
        if (this.giftExchangeLimit.ivalue === true || this.giftExchangeLimit.ivalue === 1) {
          this.giftExchangeLimit.ivalue = 1
        } else {
          this.giftExchangeLimit.ivalue = 0
        }
        querydata = Object.assign({}, this.giftExchangeLimit)
      }
      if (name === 'giftExchangeLimitMin') {
        querydata = Object.assign({}, this.giftExchangeLimitMin)
      }
      if (name === 'giftExchangeLimitMax') {
        querydata = Object.assign({}, this.giftExchangeLimitMax)
      }
      if (name === 'giftDayExMax') {
        if (this.giftDayExMax.ivalue === true || this.giftDayExMax.ivalue === 1) {
          this.giftDayExMax.ivalue = 1
        } else {
          this.giftDayExMax.ivalue = 0
        }
        querydata = Object.assign({}, this.giftDayExMax)
      }
      if (name === 'giftDayOrderMax') {
        if (this.giftDayOrderMax.ivalue === true || this.giftDayOrderMax.ivalue === 1) {
          this.giftDayOrderMax.ivalue = 1
        } else {
          this.giftDayOrderMax.ivalue = 0
        }
        querydata = Object.assign({}, this.giftDayOrderMax)
      }
      return httpAction(url, querydata, method)
    },
    getGiftParameter(name) {
      return getAction('/gift/parameter', { name: name })
    },
    getParameter(name) {
      return getAction('/bis/bisSysParameter/list', { name: name })
    },
    list() {
      getAction('/gift/workunits').then((res) => {
        if (res.success) {
          this.model = res.result.records[0]
        } else {
          this.$message.error(res.message)
        }
      })
    },
    submitForm() {
      let that = this
      that.model.coinExchangeValid = Number(that.model.coinExchangeValid)
      that.model.ticketExchangeValid = Number(that.model.ticketExchangeValid)
      that.model.pointExchangeValid = Number(that.model.pointExchangeValid)
      that.model.acoinExchangeValid = Number(that.model.acoinExchangeValid)
      that.model.aticketExchangeValid = Number(that.model.aticketExchangeValid)
      if (!(that.model.coinExchangeValid || that.model.ticketExchangeValid || that.model.pointExchangeValid || that.model.acoinExchangeValid || that.model.aticketExchangeValid )) {
        that.$message.warn(that.$t('至少使用一种兑换价值！'))
        that.confirmLoading = false
        return
      }
      if (that.model.coinExchangeValid && !that.model.coinExchange) {
        that.$message.warn(that.$t('请输入币值'))
        that.confirmLoading = false
        return
      }
      if (that.model.ticketExchangeValid && !that.model.ticketExchange) {
        that.$message.warn(that.$t('请输入彩票值'))
        that.confirmLoading = false
        return
      }
      if (that.model.pointExchangeValid && !that.model.pointExchange) {
        that.$message.warn(that.$t('请输入积分值'))
        that.confirmLoading = false
        return
      }
      if (that.model.acoinExchangeValid && !that.model.acoinExchange) {
        that.$message.warn(that.$t('请输入A币值'))
        that.confirmLoading = false
        return
      }
      if (that.model.aticketExchangeValid && !that.model.aticketExchange) {
        that.$message.warn(that.$t('请输入A票值'))
        that.confirmLoading = false
        return
      }
      putAction('/gift/workunits', that.model).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          that.list()
        } else {
          that.$message.warning(res.message)
        }
      })
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>